CREATE TABLE [dbo].[tb_ParametrosValores]
(
[idvalorparametro] [int] NOT NULL IDENTITY(1, 1),
[idparametro] [int] NOT NULL,
[tipo] [int] NOT NULL,
[valor] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[validezhasta] [datetime] NULL
) ON [PRIMARY]
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
CREATE TRIGGER [dbo].[FechaValidez] ON [dbo].[tb_ParametrosValores] 
FOR INSERT
AS
SET NOCOUNT ON
If Exists(SELECT * FROM Tb_ParametrosValores WHERE IdParametro = (Select IdParametro From Inserted) AND ValidezHasta is null)
BEGIN 
	Update Tb_ParametrosValores SET ValidezHasta = (select ValidezHasta From Inserted) 
		WHERE  (IdParametro = (Select IdParametro From Inserted) AND ValidezHAsta is null)
		
	Update Tb_ParametrosValores SET ValidezHasta = NULL 
		WHERE 	IdParametro = (Select IdParametro From Inserted)
			AND IdValorParametro = (Select IdValorParametro From Inserted)	
END



GO
ALTER TABLE [dbo].[tb_ParametrosValores] ADD CONSTRAINT [PK_Tb_ParametrosValores] PRIMARY KEY CLUSTERED  ([idvalorparametro]) ON [PRIMARY]
GO
ALTER TABLE [dbo].[tb_ParametrosValores] WITH NOCHECK ADD CONSTRAINT [FK_Tb_ParametrosValores_tb_Parametros1] FOREIGN KEY ([idparametro]) REFERENCES [dbo].[tb_Parametros] ([idParametro])
GO
ALTER TABLE [dbo].[tb_ParametrosValores] WITH NOCHECK ADD CONSTRAINT [FK_Tb_ParametrosValores_tb_TiposParametros] FOREIGN KEY ([tipo]) REFERENCES [dbo].[tb_TiposParametros] ([idTipoParametro])
GO
